/*
 * Copyright (C) 2009 Tom Barber
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
 *
 * @created Apr 23, 2009 
 * @author Tom Barber
 */


package com.encon.projecttracker.client;

import java.util.Iterator;
import java.util.List;

import com.encon.projecttracker.client.util.factory.ServiceFactory;
import com.encon.projecttracker.rpc.beans.TestDTO;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 *
 * @author tom(at)wamonline.org.uk
 */

public class Projecttracker implements EntryPoint { 
	/*
     * (non-Javadoc)
     *
     * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
     */
    /**
	 *
	 */
    /**
     * Global Session ID.
     */
    private static String sessionid;
    
    public static String getSessionID() {
	return sessionid;
    }
    /**
     * Sets the SESSION_ID.
     */
    
    private static void setSessionID() {
	ServiceFactory.getSessionInstance().createSession(
		new AsyncCallback<String>() {

		    public void onFailure(final Throwable arg0) {
			arg0.getLocalizedMessage();
		    }

		    public void onSuccess(final String arg0) {
			sessionid = arg0;
		    }

		});

    }
    
    
    public final void onModuleLoad() {
	  setSessionID();
	  HorizontalPanel horizPanel = new HorizontalPanel();
	  Button button = new Button("Fire Query");
	  final FlexTable flexTable = new FlexTable();
	  horizPanel.add(button);
	  horizPanel.add(flexTable);
	  button.addClickListener(new ClickListener(){
	      
	    public void onClick(Widget arg0) {
		// TODO Auto-generated method stub
		ServiceFactory.getQueryInstance().getRecords(getSessionID(), new AsyncCallback<List<TestDTO>>(){

			public void onFailure(Throwable arg0) {
				// TODO Auto-generated method stub
				arg0.printStackTrace();
			}

			public void onSuccess(List<TestDTO> arg0) {
				// TODO Auto-generated method stub
				for (int i=0; i < arg0.size(); i++){
				    TestDTO element = arg0.get(i);
				    
				    HTML list = new HTML(element.getColumn1());
				    
				    flexTable.setWidget(i, 0, list);
				}
				
			}

			
		
	});

	    }
	      
	  });
	  
	  RootPanel.get("splash_loading").add(horizPanel);
	
    }
    
}
